<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加商品</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <!--引入Layui的js库,模块化方式(推荐)，不用导入layui.all.js-->
    <script src="layui/layui.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="layui-container" style="margin-top: 20px">
    <div class="layui-row">
        <div class="layui-col-md6 layui-col-md-offset3">
            <form class="layui-form"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
                <div class="layui-form-item">
                    <label class="layui-form-label">商品名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="gname" placeholder="商品名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">产地</label>
                    <div class="layui-input-block">
                        <input type="text" name="producer" placeholder="产地" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">价格</label>
                    <div class="layui-input-block">
                        <input type="text" name="price" placeholder="价格" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">商品类别</label>
                    <div class="layui-input-block">
                        <select id="categorylevelone" name="categorylevelone"><!--lay-filter="category"-->
                            <!--                            <option value="0">水果</option>-->
                            <!--                            <option value="1">香蕉</option>-->
                            <!--                            <option value="2">精品香蕉</option>-->
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">请填写描述</label>
                    <div class="layui-input-block">
                        <textarea name="lable" placeholder="请输入内容" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="addGood">添加</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>

            </form>
        </div>
    </div>
</div>

<script>
    layui.use(['layer', 'form', 'laydate'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        $.get("findGoodsCategory", function (res) {
            console.log("res=", res);
            var list = res.data;
            var str = "<option value=''></option>";
            for (var i = 0; i < list.length; i++) {
                str += "<option value='" + list[i].id + "'>" + list[i].name + "</option>"
            }
            console.log('str=' + str);
            $("#categorylevelone").html(str);

            //form.render();————更新全部
            //重新渲染下拉列表
            form.render('select'); //刷新select选择框渲染
        })
    })
    layui.use(['form','layer','jquery'],function () {
        var layer=layui.layer;
        var form=layui.form;
        var $=layui.jquery;
        form.on('submit(addGood)',function (obj) {
            console.log("obj=",obj.field);
            //注意此处categorylevelone的传值
            //AJAX请求
            $.post("addGood",obj.field,function (d) {
                console.log(d);
                if (d!=1){   //插入不成功
                    layer.msg(d);
                }else {      //插入成功
                    //关闭弹出层
                    // layer.getFrameIndex(windowName) - 获取特定iframe层的索引
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                }
            },"json");
            //取消form提交的默认行为
            return false;
        });
    });
</script>
</body>
</html>